package com.yand.disk.demo.utils;


import redis.clients.jedis.Jedis;

import java.io.IOException;

/**
 * redis 校验文件是否可以下载
 */
public class SimpleRateLimter {

    private Jedis jedis;

    public SimpleRateLimter(Jedis jedis){
        this.jedis = jedis;
    }


    /**
     * 限制下载次数
     * @param flag   true 为该文件总下载次数， false为 该人对该文件下载次数
     * @param userId
     * @param fileId
     * @param maxCount
     * @return
     * @throws IOException
     */
    public boolean isActionAllowed(String userId, String fileId, int maxCount, boolean flag) throws IOException{

        String key = fileId;
        if(!flag){
            key +=":"+userId;
        }

        return jedis.incr(key) <= maxCount;
    }



}
